#include <iostream>
#include "../comm/httplib.h"
#include "../comm/util.hpp"
#include "oj_control.hpp"
using namespace httplib;
using namespace ns_util;
using namespace ns_control;

void test()
{
    // //测试Split
    // std::string str = "hello         world";
    // std::vector<std::string> vs;
    // StringUtil::Split(str, vs, ' ', false);
    // for(int i = 0; i < vs.size(); ++i)
    // {
    //     std::cout << "vs[" << i << "] = " << vs[i] << std::endl;
    // }
}

void Usage()
{
    std::cout << "Please Enter IP Port" << std::endl;
    exit(-1);
}

int main(int args, char* argv[])
{
    // test();
    
    // if(args != 2)
    // {
    //     Usage();
    // }

    Server svr;
    Controller ctl;
    /**
     * 注册资源路由
     * 获取所有题目列表
     *
     * */
    svr.Get("/all_question", [&ctl](const Request &req, Response &resp)
            {
        std::string _html;
        //LOG(INFO) << "申请所有题目列表：" << std::endl; 
        ctl.AllQuestion(_html);
        resp.set_content(_html, "text/html;charset=utf-8"); 
        });


    /**
     * 注册资源路由
     * 获取指定题目
     * \d 正则表达式，匹配多个数字
     * */

    svr.Get(R"(/question/(\d+))", [&ctl](const Request &req, Response &resp)
            {
        std::cout << "收到请求报文, url: " << req.path << std::endl;
        std::string number = req.matches[1];
        LOG(INFO) << "申请获取题目编号：" << number << std::endl; 
        std::string _html;
        ctl.Question(number, _html);
        resp.set_content(_html, "text/html;charset=utf-8"); });

    /***
     * 注册资源路由
     * 提交代码, 测试用例
     */

    svr.Get("/judge", [](const Request &req, Response &resp)
            {
        LOG(INFO) << "GET: /judge" << std::endl;
        resp.set_content("you can see me", "text/plain;charset=utf-8"); });


    svr.Post("/judge", [](const Request &req, Response &resp)
            {
        LOG(INFO) << "POST: /judge" << std::endl;
        resp.set_content("you can see me", "text/plain;charset=utf-8"); });


    // R"(/question/(\d+))"
    svr.Post(R"(/judge/(\d+))", [&ctl](const Request &req, Response &resp)
             {
                //  LOG(INFO) << "编译运行路由" << std::endl;
                 std::string number = req.matches[1];
                 std::string out_json;
                 ctl.Judge(number, req.body, out_json);
                 resp.set_content(out_json, "application/json;charset=utf-8");
             });

    svr.set_base_dir("./wwwroot");
    svr.listen("0.0.0.0", 8080);
}
